<template>
  <div class="app-container">
    <!-- 条件栏 -->
    <div v-show="!detailInfo.visible">
      <div class="filter-wrapper">
        <new-page-filter
          v-model:query="filterInfo.query.queryMap"
          :query-more-json="true"
          :filter-list="filterInfo.searchList"
          :filter-btn="filterInfo.searchBtn"
          :clear-type="clearType"
          :slipSelectRequest="slipSelectRequest"
        >
        </new-page-filter>
      </div>
      <div class="filter-wrapper">
        <div></div>
        <div class="display-flex">
          <el-button @click="preferenceInfo.visible = true">偏好设置</el-button>
        </div>
      </div>
      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :row-class-name="tableRowClassName"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-orderNo="scope">
          <span
            v-if="scope.row.orderId !== -2 && scope.row.url"
            class="slip-edit"
            @click="turnPage(scope.row)"
            >{{ scope.row.orderNo || '-' }}</span
          >
          <span v-else>{{ scope.row.orderNo || '-' }}</span>
        </template>
      </TableContainer>
    </div>
    <SetPreference
      v-if="preferenceInfo.visible"
      v-model:visible="preferenceInfo.visible"
      :search-list="filterInfo.searchList"
      :field-list="tableInfo.fieldList"
      :customize-user-v-o="filterInfo.query.customizeUserVO"
      :group-level-show="false"
      @callback="setPreferenceCallback"
    />
    <AdvancedSearch
      v-if="advancedSearchInfo.visible"
      v-model:visible="advancedSearchInfo.visible"
      v-model:form-data="advancedSearchInfo.data"
      :field-list="tableInfo.fieldList"
      @callback="advancedSearchCallback"
    />
    <modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :row="modifyInfo.data"
      @callback="getList"
    ></modify>
    <!-- <detailVue
      v-model:visible="detailInfo.visible"
      v-model:tableRow="detailInfo.data"
      @returnBack="returnBack"
    ></detailVue> -->
  </div>
</template>
<script setup name="freezeRecords">
import { getDepotFrozenStockPage } from '@/api/dataQuery'
import { getCustomizeSetting, updateUserCustomize } from '@/api/customize'
import { getSupplierAll, getDepotAll } from '@/api/core'
import modify from './modify'
// import detailVue from './detail.vue'

const { proxy } = getCurrentInstance()

const filterInfo = ref({
  sortArr: [],
  copyQueryMap: {},
  query: {
    sortString: '',
    customizeUserVO: {},
    queryMap: {
      depotAuth: {
        asTbName: 'depot_frozen_stock',
        columnName: 'depot_id',
        queryType: 'IN',
        queryCategory: 'AND',
        value: 'depotAuth',
      },
      // supplierAuth: {
      //   asTbName: 'depot_frozen_stock',
      //   columnName: 'supplier_id',
      //   queryType: 'IN',
      //   queryCategory: 'AND',
      //   value: 'supplierAuth',
      // },
    },
  },
  searchList: [],
  btnList: [],
  searchBtn: [
    { type: 'button', label: '搜索', event: _search },
    { type: 'button', label: '高级查询', event: _advancedSearchShow },
    { type: 'button', label: '重置', event: _clearSearch },
  ],
})
const modifyInfo = ref({
  visible: false,
  data: {},
})
const preferenceInfo = ref({
  visible: false,
})
const advancedSearchInfo = ref({
  visible: false,
  data: {},
})
const detailInfo = ref({
  visible: false,
  data: {},
})
const clearType = ref(true)
const slipSelectRequest = ref(true)
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  selections: [],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '替换',
        event: hanleChange,
        show: proxy.$permission('freezeRecords:replace'),
      },
    ],
  },
})

function _search() {
  search()
}
function _advancedSearchShow() {
  advancedSearchShow()
}
function _clearSearch() {
  clearSearch()
}
function hanleChange(row) {
  modifyInfo.value.data = row
  modifyInfo.value.visible = true
}
function setPreferenceCallback(obj) {
  updateUserCustomize(obj.customizeUserVO).then((res) => {
    if (res && res.success) {
      getCustomize(!slipSelectRequest.value)
    }
  })
}
function advancedSearchShow() {
  advancedSearchInfo.value.visible = true
}
// 获取列表
async function getList() {
  tableInfo.value.refresh = Math.random()
}
// 搜索
function search() {
  // 重置分页
  advancedSearchInfo.value.data = {}
  filterInfo.value.query.queryDtoList = []
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function clearSearch() {
  clearType.value = !clearType.value
  advancedSearchInfo.value.data = {}
  filterInfo.value.query.queryMap = proxy.$fn.deepClone(
    filterInfo.value.copyQueryMap
  )
  search()
}
function returnBack() {
  detailInfo.value.visible = false
  detailInfo.value.data = {}
  search()
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
function tableRowClassName({ row }) {
  let rowId = sessionStorage.getItem('storeRowId')
  if (rowId && row.id === Number(rowId)) {
    return 'check-row'
  }

  return ''
}

//展示明细
function showDetail(row) {
  sessionStorage.setItem('storeRowId', row.id)
  // detailInfo.value.visible = true
  // detailInfo.value.data = row

  // let name = ''
  // let routes = proxy.$store.state.permission.routes
  // routes.map((item) => {
  //   if (item.children && item.children.length) {
  //     item.children.map((i) => {
  //       if (i.url === url) {
  //         name = url
  //       }
  //     })
  //   }
  // })
}
//跳转
const turnPage = (data) => {
  let url = data.url
  let name = ''
  let routes = proxy.$store.state.permission.routes
  routes.map((item) => {
    if (item.children && item.children.length) {
      item.children.map((i) => {
        if (i.url === url) {
          name = url
        }
      })
    }
  })

  if (name) {
    proxy.$router.push(url)
    proxy.$store.dispatch('settings/toPage', data)
  } else {
    proxy.$message({
      duration: 3500,
      type: 'warning',
      showClose: true,
      message: '你没有相应的菜单权限',
    })
  }
}
async function advancedSearchCallback(obj) {
  filterInfo.value.query.queryMap = {}
  let arr = []
  filterInfo.value.query.queryDtoList = [...obj.queryDtoList, ...arr]
  advancedSearchInfo.value.data = obj.data
  await getList()
  clearType.value = !clearType.value
  filterInfo.value.query.queryMap = proxy.$fn.deepClone(
    filterInfo.value.copyQueryMap
  )
}

function getCustomize(_slipSelectRequest = true) {
  let param = {
    tableKey:
      'DepotFrozenStockVOList-com.slip.spd.entity.vo.stock.DepotFrozenStockVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )
      console.log(tableInfo.value.fieldList)
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.value.searchList,
        data.detail
      )
      filterInfo.value.query.queryMap = {
        ...searchInfo.queryMap,
        ...filterInfo.value.query.queryMap,
      }
      filterInfo.value.searchList = searchInfo.searchList
      filterInfo.value.searchList.forEach((item) => {
        item.event = search
      })
      filterInfo.value.copyQueryMap = proxy.$fn.deepClone(
        filterInfo.value.query.queryMap
      )
      slipSelectRequest.value = _slipSelectRequest
      tableInfo.value.api = getDepotFrozenStockPage
      getList()
    }
  })
}
let obj = {
  statusValue: {
    label: '状态',
    value: 'statusValue',
    asValue: 'status',
    componentType: 'select-model',
    code: 'province_platform_order_status',
    show: false,
    hidden: true,
  },
  qty: {
    label: '冻结数量',
    value: 'qty',
    asValue: 'qty',
    type: 'column',
    componentType: 'number',
    formatter: proxy.$fn.qtyFormatter,
    show: false,
    hidden: true,
  },
  frozenTypeValue: {
    label: '冻结类型',
    value: 'frozenTypeValue',
    asValue: 'frozenType',
    componentType: 'select-model',
    code: 'frozen_type',
    show: false,
    hidden: true,
  },
  orderTypeValue: {
    label: '业务类型',
    value: 'orderTypeValue',
    asValue: 'orderType',
    componentType: 'select-model',
    code: 'order_type',
    show: false,
    hidden: true,
  },
  frozenUserName: {
    label: '冻结人',
    value: 'frozenUserName',
    asValue: 'frozenUser',
    componentType: 'select-user',
    show: false,
    hidden: true,
  },
  frozenTime: {
    label: '冻结时间',
    value: 'frozenTime',
    asValue: 'frozenTime',
    show: false,
    hidden: true,
  },
}
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
})
filterInfo.value.searchList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
})
getCustomize()
</script>

<style scoped lang="scss"></style>
